import logging
import os
import codecs
from tqdm import tqdm
from bs4 import BeautifulSoup
import pandas as pd


def replace_char(string, char, index):
    string = list(string)
    string[index] = char
    return ''.join(string)


def read_langs(file_name):
    logging.info(("Reading lines from {}".format(file_name)))
    total_data = []

    with codecs.open(file_name, "r", "utf-8") as file:

        data = file.read()
        soup = BeautifulSoup(data, 'html.parser')
        results = soup.find_all('sentence')
        for item in tqdm(results):

            text = item.find("text").text.strip()
            correct_text = text
            mistakes = item.find_all("mistake")

            locations = []
            for mistake in mistakes:
                location = mistake.find("location").text.strip()
                wrong = mistake.find("wrong").text.strip()
                correction_word = mistake.find('correction').text.strip()
                correct_text = replace_char(correct_text, correction_word, int(location) - 1)
                locations.append(int(location))
                if text[int(location) - 1] != wrong:
                    print("The character of the given location does not equal to the real character")

            sen = list(text)
            tags = ["0" for _ in range(len(sen))]

            for i in locations:
                tags[i - 1] = "1"
            total_data.append([correct_text, text, " ".join(tags)])

    return total_data


file_name = './extra_train_ds/train.sgml'
total_data = read_langs(file_name)